/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
#include "mpu6050.h"
#include "stdio.h"
#include "adc.h"
#include "tim.h"
#include "encode.h"
#include "pid.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define line_drive 1
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
Mpu6050_Str Mpu6050_Data_Gyro;
Mpu6050_Str Mpu6050_Data_Acc;
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for ledtask1 */
osThreadId_t ledtask1Handle;
const osThreadAttr_t ledtask1_attributes = {
  .name = "ledtask1",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityRealtime7,
};
/* Definitions for ledtask2 */
osThreadId_t ledtask2Handle;
const osThreadAttr_t ledtask2_attributes = {
  .name = "ledtask2",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow3,
};
/* Definitions for uart_receive1 */
osThreadId_t uart_receive1Handle;
const osThreadAttr_t uart_receive1_attributes = {
  .name = "uart_receive1",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityRealtime6,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void Startledtask1(void *argument);
void Startledtask2(void *argument);
void Startuart_receive1(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of ledtask1 */
  ledtask1Handle = osThreadNew(Startledtask1, NULL, &ledtask1_attributes);

  /* creation of ledtask2 */
  ledtask2Handle = osThreadNew(Startledtask2, NULL, &ledtask2_attributes);

  /* creation of uart_receive1 */
  uart_receive1Handle = osThreadNew(Startuart_receive1, NULL, &uart_receive1_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    //pf9 is the blue led,进行翻转
    HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_9);
    osDelay(1000);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_Startledtask1 */
/**
* @brief Function implementing the ledtask1 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Startledtask1 */
void Startledtask1(void *argument)
{
  /* USER CODE BEGIN Startledtask1 */
  uint8_t flag;
  /* Infinite loop */
  for(;;)
  {
    //pf10 is the red led,进行翻转
    flag = MPU_Get_Gyroscope(&Mpu6050_Data_Gyro);
    if(0 == flag)
    {
      // printf("success gyro\n");
      // printf("Gyro_X:%d\n", Mpu6050_Data_Gyro.X_data);
      // printf("Gyro_Y:%d\n", Mpu6050_Data_Gyro.Y_data);
      // printf("Gyro_Z:%d\n", Mpu6050_Data_Gyro.Z_data);
    }
    flag = MPU_Get_Accelerometer(&Mpu6050_Data_Acc);
    if(0 == flag)
    {
      // printf("success acc\n");
      // printf("Acc_X:%d\n", Mpu6050_Data_Acc.X_data);
      // printf("Acc_Y:%d\n", Mpu6050_Data_Acc.Y_data);
      // printf("Acc_Z:%d\n", Mpu6050_Data_Acc.Z_data);
    }
    HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
    //  printf("hello world\n");
    //使用ADC1 通道11 读取电压�?????
    //  HAL_ADC_Start(&hadc1);
    // HAL_ADC_PollForConversion(&hadc1, 1000);
    // uint32_t adc_value = HAL_ADC_GetValue(&hadc1);
    // printf("ADC Value: %d\n", adc_value);
    //打印轮子速度
    printf("Encode1:%d.%d\n", (int)Moto1.Current_Speed,(int)(Moto1.Current_Speed*1000)%1000);
    printf("Encode2:%d.%d\n", (int)Moto2.Current_Speed,(int)(Moto2.Current_Speed*1000)%1000);
    printf("left_sensor:%d\n", light.left_light);
    printf("mid_sensor:%d\n", light.mid_light);
    printf("right_sensor:%d\n", light.right_light);
    HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_values, 3);
    // HAL_UART_Transmit(&huart1, (uint8_t *)"hello world\n", 12, 1000);
    osDelay(1000);
  }
  /* USER CODE END Startledtask1 */
}

/* USER CODE BEGIN Header_Startledtask2 */
/**
* @brief Function implementing the ledtask2 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Startledtask2 */
void Startledtask2(void *argument)
{
  /* USER CODE BEGIN Startledtask2 */
  /* Infinite loop */
  for(;;)
  {
    //设计定时器三的�?�道3的占空比
    //PID直线行驶
    #if line_drive
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, HuanyuRobot_PID_Control(&Moto1));
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, HuanyuRobot_PID_Control(&Moto2));
    //PID循迹行驶
    #else
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, HuanyuRobot_PID_Control(&Moto1));
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, HuanyuRobot_PID_Control(&Moto2));
    #endif

    osDelay(10);
  }
  /* USER CODE END Startledtask2 */
}

/* USER CODE BEGIN Header_Startuart_receive1 */
/**
* @brief Function implementing the uart_receive1 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Startuart_receive1 */
void Startuart_receive1(void *argument)
{
  /* USER CODE BEGIN Startuart_receive1 */
  /* Infinite loop */
  for(;;)
  {
    HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 2);
    osDelay(100);
  }
  /* USER CODE END Startuart_receive1 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

